<?php
	session_start();	//start session
	if(!isset($_SESSION['user_name'])){	//check if the user data is in the session
		header("location: login.php");	//if not redirect back to user
	}
?>


<html>
	<head>
		<title></title>
		<link rel="stylesheet" href="style.css">
		<script src="jquery-1.11.0.js"></script>
		<script src="gen.js"></script>
		
		<script>
			var currID;
			var viewID;
			var start=0;
			var end=4;
			var total_entries;
			
			var limit=4;
			//latest_table();
			//console.log(latest_table());

			//makes a synchronous call to the page u and return the 
			//result as object
			function syncAjax(u){

				 var obj=$.ajax(
				{
					url:u,
					async:false
			 	}
			);
				//console.log($.parseJSON(obj.responseText));
				return $.parseJSON(obj.responseText);
			}
			
			function edit(obj,id,event){
				var r=getPosition(id);
				currID = id;
				if(r.result==0){
				document.getElementById("divStatus").innerHTML=r.message;
				$("#divStatus").text(r.message);	
					return;
				}


				//get the data from object r and put it in the form
				$("#position_name").prop("value", r.position.position_name);		
				$("#position_created").prop("value", r.position.position_created);
			
				//show the form
				//find where the user clicked and store it in x and y
				var y=event.clientY;
				var x=event.clientX/2;
				//use x and y to set the location of the form
				$("#divView").hide();
				$("#divEdit").hide();
				$("#divAdd").hide();
				$("#divEdit").css("top",y);
				$("#divEdit").css("left",x);
				//display the form
				$("#divEdit").fadeIn(1000);
				// $("#addid").hide();
				$("#savid").show();
				
			}
			
			function view(obj,id,event){
				var r=getPosition(id);
				viewID = id;
				if(r.result==0){
				document.getElementById("divStatus").innerHTML=r.message;
				$("#divStatus").text(r.message);	
					return;
				}


				//get the data from object r and put it in the form
				$("#p_name").prop("value", r.position.position_name);		
				$("#p_created").prop("value", r.position.position_created);
				
				//show the form
				//find where the user clicked and store it in x and y
				var y=event.clientY;
				var x=event.clientX/2;
				//use x and y to set the location of the form
				$("#divView").hide();
				$("#divEdit").hide();
				$("#divAdd").hide();
				$("#divView").css("top",152);
				$("#divView").css("left",402);
				$("#divView").fadeIn(1000);
				$("#viewid").show();
			}


			//returns a result object for one vaccine 
			function getPosition(id){
				var u="position_action.php?cmd=1&pid="+id;
				return syncAjax(u);
			}

			function search(){
				var tx=document.getElementById("txtSearch").value;
				console.log(tx);
				var u = "position_action.php?cmd=5&pn="+tx;
				r=syncAjax(u);
				search_table(r);
				
			}



			function add(){
				$("#divView").hide();
				$("#divEdit").hide();
				$("#divAdd").hide();
				var y=event.clientY;
				var x=event.clientX/3;
				$("#divAdd").css("top",152);
				$("#divAdd").css("left",402);
				$("#divAdd").fadeIn(1000);
				$("#addid").show();
				// $("#savid").hide();
			}


			
			function addNew(){
				//complete the url
				var pn=document.getElementById("pn").value;
				var pc=document.getElementById("pc").value;
				var u = "position_action.php?cmd=4&pn="+pn+"&pc="+pc;	
				r=syncAjax(u);
				//$.getJSON(u,saveDone);
				r.result==1?alert("Saved"):alert("Save failed");
				latest_table();
				cancel_add();
			}

			//makes asynchronous call to the save page
			function save(){
				//complete the url
				var pn=document.getElementById("position_name").value;
				var pc=document.getElementById("position_created").value;
				var u = "position_action.php?cmd=3&pn="+pn+"&pc="+pc+"&pid="+currID;
				r=syncAjax(u);
				$.getJSON(u,saveDone);
				//r.result==1?alert("Saved"):alert("Save failed");
				latest_table();
				cancel_edit();
			}

			
			
			function saveDone(data){
				if (data.result==1){	
				alert("Saved");
			}
			else {
				alert("Save failed");	
				}
			}
			//hides the form
			function cancel_edit(){
				//fade out the form in half a second
				$("#divEdit").fadeOut(500);
				
			}

			function cancel_add(){
				//fade out the form in half a second
				$("#divAdd").fadeOut(500);
				
			}

			function cancel_view(){
				//fade out the form in half a second
				$("#divView").fadeOut(500);
				
			}

			function next(){
				if (end<total_entries){
				$("#divNext").show();
				$("#divStatus").hide();	
				start=end;
				end+=limit;
				if (end<total_entries){
				latest_table();
			}
			else{
				end=total_entries;
				latest_table();
			}
			}
			else{
				$("#divStatus").show();	
				$("#divStatus").text("You have reached the end of the page");
			}
			
			}

			function previous(){
				if (start>0){
				$("#divPrevious").show();
				$("#divStatus").hide();		
				end=start;
				start-=limit;
				latest_table();
			}
			else{
				$("#divStatus").show();
				$("#divStatus").text("There is no position information before this page");
			}
			}


			function rotate_style(row){
				if(row%2==0){
				return " class='row1'";
				}else{
				return " class='row2'";
				}	
			}

			function latest_table(){
				var u="position_action.php?cmd=2";	
				r= syncAjax(u);		
				total_entries = r.positions.length;
				console.log(total_entries);
				var table ="<table class='reportTable'><tr class='header'><td>Name</td><td>View</td><td>Edit</td><td>Delete</td></tr>";
			
				for(var i=start;i<end;i++){

					table+="<tr"+rotate_style(i)+"><td>"+r.positions[i].position_name+"</td><td>"+"<span class='hotspot' onclick='view(this,"+r.positions[i].pid+",event)'>view</td><td>"
					+"<span class='hotspot' onclick='edit(this,"+r.positions[i].pid+",event)'>edit</span></td><td>"+
					"<a href='index.php?pid="+r.positions[i].pid+"' onclick='return confirm(\"Are you sure you want to delete?\")'>delete<a></td></tr>";

					
				}
				
				table +="<tr class='pagination'><td id='divPrevious'><span class='hotspot' onclick='previous()'>&lt; PREVIOUS PAGE </span></td><td></td><td></td><td id='divNext'><span class='hotspot' onclick='next()'>NEXT PAGE &gt;</span></td></tr></table>";

				if(document.getElementById("divContent")==null){
					console.log("Not yet initialized");
				}
				else {
				document.getElementById("divContent").innerHTML=table;
			}
				
			}

			function search_table(r){
				//var u="user_action.php?cmd=5&fn="+tx;	
				//console.log(u);
				//r= syncAjax(u);
				if (r.positions!=null){	
				console.log(r);	
				var table ="<table class='reportTable'><tr class='header'><td>Name</td><td>View</td><td>Edit</td><td>Delete</td></tr>";
			
				for(var i=0;i<r.positions.length;i++){

					table+="<tr"+rotate_style(i)+"><td>"+r.positions[i].position_name+"</td><td>"+"<span class='hotspot' onclick='view(this,"+r.positions[i].pid+",event)'>view</a></td><td>"
					+"<span class='hotspot' onclick='edit(this,"+r.positions[i].pid+",event)'>edit</span></td><td>"+
					"<a href='index.php?pid="+r.positions[i].pid+"' onclick='return confirm(\"Are you sure you want to delete?\")'>delete<a></td></tr>";
					
				}
				table +="</table>";
				if(document.getElementById("divContent")==null){
					console.log("Not yet initialized");
				}
				else {
				document.getElementById("divContent").innerHTML=table;
			}
					$("#divContent").show();
					$("#divStatus").hide();
			}
				else{

					$("#divContent").hide();
					$("#divStatus").text("Position not found!!");
					$("#divStatus").show();
					
				}
				
			}

		</script>

		<?php
			// include("users.php");
			// $obj=new users();
			// include("positions.php");	
			// $objPositions=new positions();
			// if (isset($_REQUEST['id'])){
			// 	$obj->delete_user($_REQUEST['id']);
			// }	
			// if(!$obj->get_all_users()){
			// echo "error";
			// exit();
			// }
			// $row=$obj->fetch();
		?>
	</head>
	<body>
		
		<table>
			<tr>
				<td colspan="2" id="pageheader">
					Health Information System
				</td>
			</tr>
			<tr>
				<td id="mainnav">	
					<div class="menuitem">location</div>
					<div class="menuitem">opd cases</div>
					<div class="menuitem">health promotion</div>
					<div class="menuitem">nutrition</div>
					<div class="menuitem">child welfare</div>
					<div class="menuitem">family planning</div>
					<div class="menuitem"><a href="logout.php">logout<a></div>
				</td>
				<td id="content">
					<div id="divPageMenu">
						<span class="menuitem" ><a onclick="add()">new</a></span>
						<span class="menuitem" >sub districts</span>
						<span class="menuitem" >communities</span>
						<span class="menuitem" >view map</span>
						<input type="text" id="txtSearch">
						<span class="menuitem" onclick="search()">search</span>		
					</div>
					<div id="divStatus" class="status">
	
					</div>
					<div id="divContent" name="divContent">
						<script>
						latest_table();
						</script>
					</div>
				</td>
			</tr>
		</table>



<div id="divEdit" class="popupForm">
		<table class="tableForm" >
					<?php			

							echo "<h3>Edit Position Information</h3>";
					?>
					<tr>
						<td class="label">Position Name</td> 
						<td class="field"><input type="text" value="<?php echo $row['position_name'] ?>" name="position_name" id="position_name">
						</td>
					</tr>			
					<tr>
						<td class="label">Position Created </td>
						<td class="field"><input type="text" readonly="true" value="<?php echo $row['position_created'] ?>" name="position_created" id="position_created">
						</td>
					</tr>
					<tr>
						<td class="label"></td>
						<td class="field">
							<input id="savid" type="button" value="save" onclick="save()" >
							<!-- <input id="addid" type="button" value="save" onclick="addNew()" > -->
							<input type="button" value="cancel" onclick="cancel_edit()" >
						</td>
					</tr>
			</table>
	</div>





	<div id="divAdd" class="popupForm">
		<table class="tableForm" >
					<tr>
						<td class="label">Position Name</td> 
						<td class="field"><input type="text" id="pn" name="pn">
						</td>
					</tr>
					<tr>
						<td class="label">Position Created </td>
						<td class="field"><input type="hidden" readonly="true" name="pc" id="pc">
						</td>
					</tr>
					<tr>
						<td class="label"></td>
						<td class="field">
							<!-- <input id="savid" type="button" value="save" onclick="save()" > -->
							<input id="addid" type="button" value="save" onclick="addNew()" >
							<input type="button" value="cancel" onclick="cancel_add()" >
						</td>
					</tr>
			</table>
	</div>

	<div id="divView" class="popupForm">
		<table class="tableForm" >
			<?php			

							echo "<h3>View Position Information</h3>";
					?>
					<tr>
						<td class="label">Position name</td> 
						<td class="field"><input type="text" id="p_name" value="<?php echo $row['position_name'] ?>" readonly="true" name="p_name">
						</td>
					</tr>
					<tr>
						<td class="label">Position Created </td>
						<td class="field"><input type="text" readonly="true" value="<?php echo $row['position_created'] ?>" name="p_created" id="p_created">
						</td>
					</tr>
					<tr>
						<td class="label"></td>
						<td class="field">
							
							<input type="button" value="cancel" onclick="cancel_view()" >
						</td>
					</tr>
			</table>
	</div>
	</body>

</html>	